package com.wing.order.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.model.response.TemporaryStorageExportVO;
import com.wing.common.utils.JsonResult;
import com.wing.order.dao.OrderReceiveLoadDao;
import com.wing.order.model.entity.OrderReceiveLoad;
import com.wing.order.model.request.OrderReceiveLoadForm;
import com.wing.order.model.response.OrderReceiveLoadVO;
import com.wing.order.model.response.OrderReceiveUnloadVO;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.DepartmentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 上货数量-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class OrderReceiveLoadServiceImpl extends ServiceImpl<OrderReceiveLoadDao, OrderReceiveLoad> implements OrderReceiveLoadService {

    @Autowired
    private DynamicService dynamicService;


//    @Autowired
//    private CellInventoryInoutDetailService cellInventoryInoutDetailService;

//    @Autowired
//    private OrderReceiveDetailService orderReceiveDetailService;

    @Override
    public int add(OrderReceiveLoadForm orderReceiveLoadForm) {

        //校验

        OrderReceiveLoad orderReceiveLoad =new OrderReceiveLoad();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(OrderReceiveLoad.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(orderReceiveLoadForm, orderReceiveLoad, copyOptions);

        return baseMapper.insert(orderReceiveLoad);
    }

    @Override
    public List<OrderReceiveLoadVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<OrderReceiveLoadVO> pageList4app(Page page, Map<String, Object> params) {
        List<OrderReceiveLoadVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public OrderReceiveLoadVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional
    public void load(OrderReceiveLoadForm orderReceiveLoadForm) {
        /**
        List<OrderReceiveDetail> receiveDetails=orderReceiveDetailService.list(new QueryWrapper<OrderReceiveDetail>().eq("receive_id",orderReceiveLoadForm.getReceiveId()).eq("sku_code",orderReceiveLoadForm.getSkuCode()));
        if (receiveDetails.size()==0){
            throw new BusinessException("未找到领货明细业务数据");
        }

//        OrderReceiveLoad orderReceiveLoad=new OrderReceiveLoad();
        BeanUtils.copyProperties(orderReceiveLoadForm,orderReceiveLoad);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code=dynamicService.getDayNextCode("ord_order_receive_load");
        String day=simpleDateFormat.format(new Date());
        orderReceiveLoad.setCode("RL-"+day+"-"+code);
        orderReceiveLoad.setDay(Integer.valueOf(day));
        orderReceiveLoad.setNo(Integer.valueOf(code));
        orderReceiveLoad.setCreator(JwtUtils.getLoginUserId());
        orderReceiveLoad.setCreatorName(JwtUtils.getLoginUsername());
        orderReceiveLoad.setReceiveDetailId(receiveDetails.get(0).getId());
        baseMapper.insert(orderReceiveLoad);



        CellInventoryInoutForm cellInventoryInoutForm=new CellInventoryInoutForm();
        cellInventoryInoutForm.setReceiveLoadId(orderReceiveLoad.getId());
        List<CellInventoryInoutDetailForm> detailList=new ArrayList<>();
        CellInventoryInoutDetailForm cellInventoryInoutDetailForm=new CellInventoryInoutDetailForm();
        cellInventoryInoutDetailForm.setCabCode(orderReceiveLoadForm.getCabCode());
        cellInventoryInoutDetailForm.setSkuCode(orderReceiveLoadForm.getSkuCode());
        cellInventoryInoutDetailForm.setSpaceCode(orderReceiveLoadForm.getSpaceCode());
        cellInventoryInoutDetailForm.setQuantity(orderReceiveLoadForm.getQuantity());
        cellInventoryInoutDetailForm.setReceiveDetailId(receiveDetails.get(0).getId());
        detailList.add(cellInventoryInoutDetailForm);
        cellInventoryInoutForm.setDetailList(detailList);
        cellInventoryInoutDetailService.inventoryIn(cellInventoryInoutForm);

        cellInventoryInoutDetailService.inventoryIn(orderReceiveLoadForm);
         */
    }

    @Override
    public List<OrderReceiveLoadVO> ecxportList(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public List<OrderReceiveUnloadVO> ecxportUnloadList(Map<String, Object> params) {
        return baseMapper.listUnload4app(params);
    }
}
